﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FJERP.Model;
using System.Data.SqlClient;
using System.Data;

namespace FJERP.DAL
{
   public  class WareHoseDAL
    {
        SQLHelper h = new SQLHelper();
        /// <summary>
        /// 显示库房
        /// </summary>
        /// <returns></returns>
        public List<WareHose> ShowWareHose()
        {

            List<WareHose> list = new List<WareHose>();
            DataTable data = h.ExecDataTable_Proc("WareHose_show");
            foreach (DataRow item in data.Rows)
            {
                WareHose w = new WareHose();
                w.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
                w.WareHose_Name = item["WareHose_Name"].ToString();
                w.WareHose_People = item["WareHose_People"].ToString();
                w.WareHose_Phone = item["WareHose_Phone"].ToString();
                w.WareHose_Address = item["WareHose_Address"].ToString();
                w.WareHose_Remark = item["WareHose_Remark"].ToString();
                list.Add(w);
            }
            return list;
        }
        /// <summary>
        /// 添加商品库房
        /// </summary>
        /// <param name="w"></param>
        /// <returns></returns>
        public int AddWareHose(WareHose w)
        {
            string warehose = "WareHose_Add";
            SqlParameter[] par = new SqlParameter[] {
                   new  SqlParameter("@name",w.WareHose_Name),
                   new  SqlParameter("@people",w.WareHose_People),
                   new  SqlParameter("@phone",w.WareHose_Phone),
                   new  SqlParameter("@address",w.WareHose_Address),
                   new  SqlParameter("@remark",w.WareHose_Remark),
            };
            return h.ExecNonquery_Proc(warehose, par);
        }
        ///<summary>
        /// 删除仓库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Del(int id)
        {
            string sql = "delete WareHose where WareHose_Id=" + id + "";
            return h.ExecNonquery(sql);
        }
        /// <summary>
        /// 根据编号ID获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetWareHoseById(int id)
        {
            string sql = "select WareHose_Id,WareHose_Name,WareHose_People,WareHose_Phone,WareHose_Address,WareHose_Remark from WareHose where WareHose_Id=" + id + "";
            return h.ExecDataTable(sql);
        }
        /// <summary>
        /// 根据名称查询
        /// </summary>
        /// <param name="WareHose_Name"></param>
        /// <returns></returns>
        public List<WareHose> SeaarchWareHose(string WareHose_Name)
        {
            string sql = "select WareHose_Id,WareHose_Name,WareHose_People,WareHose_Phone,WareHose_Address,WareHose_Remark from WareHose where 1=1";
            if (!string.IsNullOrEmpty(WareHose_Name))
            {
                sql += "and WareHose_Name like '%" + WareHose_Name + "%'";
            }
            List<WareHose> list = new List<WareHose>();
            DataTable data = h.ExecDataTable(sql);
            foreach (DataRow item in data.Rows)
            {
                WareHose w = new WareHose();
                w.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
                w.WareHose_Name = item["WareHose_Name"].ToString();
                w.WareHose_People = item["WareHose_People"].ToString();
                w.WareHose_Phone = item["WareHose_Phone"].ToString();
                w.WareHose_Address = item["WareHose_Address"].ToString();
                w.WareHose_Remark = item["WareHose_Remark"].ToString();
                list.Add(w);
            }
            return list;
        }
        
        /// <summary>
        /// 修改数据信息
        /// </summary>
        /// <param name="w"></param>
        /// <returns></returns>
        public int UpdWareHose(WareHose w)
        {
            string updatewar = "warehose_update";
            SqlParameter[] par = new SqlParameter[] {
                   new  SqlParameter("@id",w.WareHose_Id),
                   new  SqlParameter("@name",w.WareHose_Name),
                   new  SqlParameter("@people",w.WareHose_People),
                   new  SqlParameter("@phone",w.WareHose_Phone),
                   new  SqlParameter("@adress",w.WareHose_Address),
                   new  SqlParameter("@remark",w.WareHose_Remark),
            };
            return h.ExecNonquery_Proc(updatewar, par);
        }
       /// <summary>
       /// 总条数
       /// </summary>
       /// <returns></returns>
        public int countWareHose()
        {
            string sql = "select count(*) from WareHose";
            return Convert.ToInt32(h.ExecScalar(sql));
        }
    }
}
